package com.portal.cwegportal.service;

import com.appcenter.jz.unit.ObjectUnit;
import com.portal.cwegportal.input.functionmap.*;
import com.portal.cwegportal.model.FunctionMap;
import com.portal.cwegportal.output.functionmap.*;
import com.appcenter.jz.page.PagedResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import  com.portal.cwegportal.mapper.*;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
/**
 *  创建人:王增立
 *  备注: 功能地图
 *  时间: 2019/4/26 15:01
 *  参数: 
 */
@Service
public class FunctionMapService {
    @Autowired
    FunctionMapMapper functionMapMapper;
    @Transactional
    /**
     *  创建人:王增立
     *  备注: 创建功能地图
     *  时间: 2019/4/26 15:02
     *  参数: 
     */
    public CreateFunctionMapOutput createFunctionMap(CreateFunctionMapInput input) throws Exception {
        CreateFunctionMapOutput output = new CreateFunctionMapOutput();
        FunctionMap functionMap=new FunctionMap();
        ObjectUnit.Copy(input.getItem(),functionMap);
        functionMap.setGmtCreate(new Date());
        functionMap.setGmtModified(new Date());
        functionMap.setDeleted(false);
        functionMapMapper.insertSelective(functionMap);
        output.setItem(functionMap);
        return output;
    }
/**
 *  创建人:王增立
 *  备注: 编辑功能地图
 *  时间: 2019/4/26 15:02
 *  参数: 
 */
    @Transactional
    public EditFunctionMapOutput editFunctionMap (EditFunctionMapInput input) throws Exception{
        EditFunctionMapOutput output=new EditFunctionMapOutput();
        FunctionMap oldEntity= functionMapMapper.selectByPrimaryKey(input.getItem().getId());
        oldEntity.setFunctionDesc(input.getItem().getFunctionDesc());
        oldEntity.setFunctionName(input.getItem().getFunctionName());
       oldEntity.setOnlineTime(input.getItem().getOnlineTime());
       oldEntity.setVersion(input.getItem().getVersion());
       oldEntity.setSortNo(input.getItem().getSortNo());
       oldEntity.setGmtModified(new Date());
        functionMapMapper.updateByPrimaryKey(oldEntity);
        output.setItem(oldEntity);
        return  output;
    }

/**
 *  创建人:王增立
 *  备注: 删除功能地图
 *  时间: 2019/4/26 15:02
 *  参数: 
 */
    public DeleteFunctionMapOutput deleteFunctionMap(DeleteFunctionMapInput input) {
        DeleteFunctionMapOutput output = new DeleteFunctionMapOutput();
        FunctionMap oldEntity = functionMapMapper.selectByPrimaryKey(input.getItem().getId());
        oldEntity.setDeleted(true);
        functionMapMapper.updateByPrimaryKey(oldEntity);
        return output;
    }
/**
 *  创建人:王增立
 *  备注: 获取功能地图
 *  时间: 2019/4/26 15:02
 *  参数: 
 */
    public GetFunctionMapsOutput getFunctionMaps(GetFunctionMapsInput input) {
        GetFunctionMapsOutput output = new GetFunctionMapsOutput();

        //logger.error("log4j2 error!");

        PageHelper.startPage(input.getPage().getPageNo(),input.getPage().getPageSize(),"Id desc");
        //List<Menus> ls = MenuMapper.SearchMenus(input);
        Example example = new Example(FunctionMap.class);
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("deleted",0);
        List<FunctionMap> ls=functionMapMapper.selectByExample(example);
        PageInfo<FunctionMap> page = new PageInfo<>(ls);
        output.setItems(new PagedResult<FunctionMap>(page.getList(),(int)page.getTotal()));
        return  output;

    }
/**
 *  创建人:王增立
 *  备注: 加载功能地图
 *  时间: 2019/4/26 15:30
 *  参数: 
 */
    public PageLoadOutput pageLoad(PageLoadInput input) {
        PageLoadOutput output = new PageLoadOutput();
        output.setItems(getFunctionMaps(input).getItems());
        return output;
    }
}
